package com.module.admin.app.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.app.dao.AppPrjDao;
import com.module.admin.app.pojo.AppPrj;
import com.module.admin.app.service.AppPrjService;
import com.module.admin.sys.utils.SysUserUtil;
import com.system.comm.model.KvEntity;
import com.system.comm.model.Page;
import com.system.handle.model.ResponseFrame;

/**
 * app_prj的Service
 * @author admin
 * @date 2020-12-27 20:12:45
 * @version V1.0.0
 */
@Component
public class AppPrjServiceImpl implements AppPrjService {

	@Autowired
	private AppPrjDao appPrjDao;
	
	/**
	 * 保存
	 * @param appPrj
	 * @return
	 */
	@Override
	public ResponseFrame save(AppPrj appPrj) {
		ResponseFrame frame = new ResponseFrame();
		AppPrj org = get(appPrj.getAiId(), appPrj.getPrjId());
		if(org == null) {
			appPrjDao.save(appPrj);
		}
		frame.setSucc();
		return frame;
	}
	
	/**
	 * 根据aiId获取对象
	 * @param aiId
	 * @return
	 */
	@Override
	public AppPrj get(String aiId, Integer prjId) {
		return handleDtl(appPrjDao.get(aiId, prjId));
	}

	/**
	 * 分页获取对象
	 * @param appPrj
	 * @return
	 */
	@Override
	public Page<AppPrj> pageQuery(AppPrj appPrj) {
		appPrj.setDefPageSize();
		int total = appPrjDao.findAppPrjCount(appPrj);
		List<AppPrj> data = null;
		if(total > 0) {
			data = appPrjDao.findAppPrj(appPrj);
			for (AppPrj item : data) {
				handleDtl(item);
			}
		}
		Page<AppPrj> page = new Page<AppPrj>(appPrj.getPage(), appPrj.getSize(), total, data);
		return page;
	}
	/**
	 * 处理字典等详细信息的方法
	 * @param appPrj
	 */
	private AppPrj handleDtl(AppPrj appPrj) {
		if (appPrj != null) {
			appPrj.setCreateUserName(SysUserUtil.getNickname(appPrj.getCreateUserId()));
		}
		return appPrj;
	}
	
	/**
	 * 根据aiId删除对象
	 * @param aiId
	 * @return
	 */
	@Override
	public ResponseFrame delete(String aiId, Integer prjId) {
		ResponseFrame frame = new ResponseFrame();
		appPrjDao.delete(aiId, prjId);
		frame.setSucc();
		return frame;
	}

	@Override
	public List<KvEntity> findSearch(String searchString, String aiId) {
		return appPrjDao.findSearch(searchString, aiId);
	}
}